-
Notifications
You must be signed in to change notification settings - Fork 450
feat(event-handler): add support for Pydantic Field discriminator in validation #7227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
…validation (aws-powertools#5953) Enable use of Field(discriminator='...') with tagged unions in event handler validation. This allows developers to use Pydantic's native discriminator syntax instead of requiring Powertools-specific Param annotations. - Handle Field(discriminator) + Body() combination in get_field_info_annotated_type - Preserve discriminator metadata when creating TypeAdapter in ModelField - Add comprehensive tests for discriminator validation and Field features
Hey @dap0am please run |
Done, thanks. |
Thanks for fixing! You can run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dap0am! Please let me know if you need any help here.
Hi @leandrodamascena nope, I am just waiting for your review. |
Hi @dap0am, can you fix the CI and make the appropriate changes so as not to break the current tests? The CI is failing because there are some refactorings that can introduce bug regressions into the existing code and break all customers using it. Use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dap0am please fix the CI before we start the review.
Will do that, thanks. |
Fix regression where copy_field_info was losing custom FieldInfo subclass types (Body, Query, etc.) by using shallow copy instead of from_annotation. This resolves the failing test_validate_embed_body_param while maintaining the discriminator functionality.
Hi @dap0am, some of the unit tests are failing - please take a look if you can and also resolve the SonarCloud issues listed in the comment above. Thanks! |
…rCloud issues - Refactor get_field_info_annotated_type function by extracting helper functions to reduce cognitive complexity from 29 to below 15 - Fix copy_field_info to preserve FieldInfo subclass types using shallow copy instead of from_annotation - Rename variable Action to action_type to follow Python naming conventions - Resolve failing test_validate_embed_body_param by maintaining Body parameter type recognition - Add helper functions: _has_discriminator, _handle_discriminator_with_body, _create_field_info, _set_field_default - Maintain full backward compatibility and discriminator functionality
|
Summary
This PR adds support for Pydantic's
Field(discriminator='...')
syntax in event handler validation, enabling tagged unions to work correctly with the validation middleware.Fixes #5953
Changes
get_field_info_annotated_type()
inparams.py
to handleField(discriminator) + Body()
combinationsModelField.__post_init__()
incompat.py
to preserve discriminator metadata when creating TypeAdaptersTesting
test_field_discriminator_validation()
to verify discriminator functionality with tagged unionstest_field_other_features_still_work()
to ensure other Field features remain functionalExample Usage
Checklist
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.